"""
# coding     : utf-8 
# Time       : 2025/6/11 10:52
# Author     : chenxianb
# version    : python 3.8.2
# Description: 通过接口获取双色球前3页的中奖记录
"""
import os.path
import time

import openpyxl
import requests


def get_json_data(url, pageNo):
    header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0",
              "Host": "www.cwl.gov.cn",
              "Referer": "https://www.cwl.gov.cn/ygkj/wqkjgg/",
              "Cookie": "HMF_CI=7232e5de5a2f4f08badfd9f1687cca74679fb1b4acab8cb44f36fbb661cfc136f47342b3fe91e35049d46cbe0359f27fcbf63b51d20a242e67bdffd5743ae51d12; 21_vq=8"
              }
    param = {"name": "ssq", "pageNo": f"{pageNo}",
             "pageSize": 30, "systemType": "PC"}
    try:
        r = requests.get(url, params=param, headers=header, timeout=30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.json()
    except:
        return ""


def get_ticket(json_data):
    """获取彩票信息"""
    info = []

    result = json_data["result"]
    for item in result:
        # 期号
        code = item["code"]
        # 开奖日期
        date = item["date"]
        # 红球
        red = item["red"]
        # 蓝球
        blue = item["blue"]
        # 销售额
        sales = item["sales"]
        # 奖池金额
        poolmoney = item["poolmoney"]
        # 公告链接
        details = item["detailsLink"]
        detailsLink = f"https://www.cwl.gov.cn/{details}"
        # 视频链接
        video = item["videoLink"]
        videoLink = f"https://www.cwl.gov.cn/{video}"
        # 备注
        content = item["content"]
        # 中奖情况
        # 设置成全局变量
        global num1, prize1, num2, prize2, prize3, num3, num4, prize4, num5, prize5, num6, prize6
        for prizegrade in item["prizegrades"]:
            if prizegrade["type"] == 1:
                num1 = prizegrade["typenum"]
                prize1 = prizegrade["typemoney"]
            elif prizegrade["type"] == 2:
                num2 = prizegrade["typenum"]
                prize2 = prizegrade["typemoney"]
            elif prizegrade["type"] == 3:
                num3 = prizegrade["typenum"]
                prize3 = prizegrade["typemoney"]
            elif prizegrade["type"] == 4:
                num4 = prizegrade["typenum"]
                prize4 = prizegrade["typemoney"]
            elif prizegrade["type"] == 5:
                num5 = prizegrade["typenum"]
                prize5 = prizegrade["typemoney"]
            elif prizegrade["type"] == 6:
                num6 = prizegrade["typenum"]
                prize6 = prizegrade["typemoney"]

        info.append(
            [code, date, red, blue, sales, poolmoney, content, num1, prize1, num2, prize2, num3, prize3, num4, prize4,
             num5, prize5, num6, prize6, detailsLink, videoLink])

    return info


def save_ticket_info(info, path):
    if not os.path.exists(path):
        os.mkdir(path)

    timestamp = int(time.time())
    file = f"{path}/双色球中奖详情{timestamp}.xlsx"

    wb = openpyxl.Workbook(file)
    table = wb.create_sheet("ticket", 0)

    title = ["期号", "开奖日期", "红球", "蓝球", "销售额(元)", "奖池金额(元)", "备注", "一等奖注数", "一等奖单注金额(元)", "二等奖注数", "二等奖单注金额(元)", "三等奖注数",
             "三等奖单注金额(元)",
             "四等奖注数", "四等奖单注金额(元)", "五等奖注数", "五等奖单注金额(元)", "六等奖注数", "六等奖单注金额(元)", "中奖公告链接", "视频详情"]

    table.append(title)

    for item in info:
        table.append(item)

    wb.save(file)
    wb.close()
    print("保存成功")


if __name__ == '__main__':
    url = "http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?"
    path = "./ticket/"
    tickets = []

    # 前三页
    for i in range(1, 4):
        time.sleep(3)
        json_data = get_json_data(url, i)
        info = get_ticket(json_data)
        tickets.extend(info)  # 通过追加的方式来添加列表

    save_ticket_info(tickets, path)
